﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserQuanXianManager
{


	public partial class MainWin : Form
	{


		public MainWin()
		{
			InitializeComponent();
		}

		private void MainWin_Load(object sender, EventArgs e)
		{
			UpdateUserSelectListBox();
			UpdateGroupSelectListBox();
			//UpdateProcessStepBox();

			UpdateActionListBox();

			//操作流程
			FillOperationTypeCommonbox();
		}

		private void UpdateUserSelectListBox()
		{
			cbUsers.Items.Clear();
			using (var context = new blackbirdEntities())
			{
				var allusers = context.user;
				foreach (var oneUser in allusers)
				{
					MyListBoxItem userItem = new MyListBoxItem();
					userItem.id = oneUser.idUser;
					userItem.Context = oneUser.Name;
					cbUsers.Items.Add(userItem);
				}
			}
		}

		private void UpdateGroupSelectListBox()
		{
			cbGroup.Items.Clear();
			using (var context = new blackbirdEntities())
			{
				var allGroup = context.group;
				foreach (var gp in allGroup)
				{
					MyListBoxItem gpItem = new MyListBoxItem();
					gpItem.id = gp.idGroup.ToString();
					gpItem.Context = gp.Name;
					cbGroup.Items.Add(gpItem);
				}
			}
		}

		private void UpdateActionListBox()
		{
			cbActions.Items.Clear();
			using (var context = new blackbirdEntities())
			{
				var allAction = context.action;
				foreach (var gp in allAction)
				{
					MyListBoxItem gpItem = new MyListBoxItem();
					gpItem.id = gp.idAction.ToString();
					gpItem.Context = gp.Name + "," + gp.Description;
					cbActions.Items.Add(gpItem);
				}
			}
		}

		private void lbUsers_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void cbUsers_SelectedIndexChanged(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cbUsers.SelectedItem as MyListBoxItem;
			if (mBoxItem != null)
			{
				lbUserGroups.Items.Clear();
				using (var context = new blackbirdEntities())
				{
					var selecteduser = from u in context.user
								   where u.idUser == mBoxItem.id
								   select u;
					var userone = selecteduser.ToList()[0];

					foreach (var gp_user in userone.group_user)
					{
						var gp = context.group.FirstOrDefault(v => v.idGroup == gp_user.Group_idGroup);
						MyListBoxItem gpItem = new MyListBoxItem();
						gpItem.id = gp.idGroup.ToString();
						gpItem.Context = gp.Name;
						lbUserGroups.Items.Add(gpItem);
					}
					
				}
			}
		}

		private void cbGroup_SelectedIndexChanged(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cbGroup.SelectedItem as MyListBoxItem;
			if (mBoxItem != null)
			{
				lbGroupActions.Items.Clear();

				int n = int.Parse(mBoxItem.id);
				using (var context = new blackbirdEntities())
				{					

					var gp_actions = context.group_action.Where(v => v.Group_idGroup == n).ToList();

					foreach (var gp_action in gp_actions)
					{						
						MyListBoxItem gpItem = new MyListBoxItem();
						gpItem.id = gp_action.id.ToString(); // save group_action id is easy to delete it later.
						gpItem.Context = gp_action.action.Name + "," + gp_action.action.Description;
						lbGroupActions.Items.Add(gpItem);
					}


				}
			}
		}

		private void AddGroupToUser_Click(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cbUsers.SelectedItem as MyListBoxItem;
			if (mBoxItem == null)
			{
				return;
			}

			GroupSelect gS = new GroupSelect();
			if (gS.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				var container = gS.GetContain;

				using (var context = new blackbirdEntities())
				{
					var currentUser = context.user.FirstOrDefault(v => v.idUser == mBoxItem.id);

					foreach (var item in container.CheckedItems)
					{
						MyListBoxItem myItem = item as MyListBoxItem;

						//Get GroupId
						int n = int.Parse(myItem.id);
						var existedgroup = currentUser.group_user.FirstOrDefault(v => v.Group_idGroup == n);
						if (existedgroup == null)
						{
							var gp_user = new group_user();
							gp_user.Group_idGroup = n;
							gp_user.User_idUser = currentUser.idUser;

							context.group_user.AddObject(gp_user);
						}						
					}
					context.SaveChanges();
				}
			}
		}

		private void AddNewUser_Click(object sender, EventArgs e)
		{
			AddNewUser newUser = new AddNewUser();
			if (newUser.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				UpdateUserSelectListBox();
			}
		}

		private void AddNewGroup_Click(object sender, EventArgs e)
		{
			AddNewGroup newGroup = new AddNewGroup();

			if (newGroup.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				UpdateGroupSelectListBox();
			}
		}

		private void btDeleteUser_Click(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cbUsers.SelectedItem as MyListBoxItem;
			if (mBoxItem == null)
			{
				return;
			}
			using (var context = new blackbirdEntities())
			{
				var user = context.user.FirstOrDefault(v => v.idUser == mBoxItem.id);

				var contact = context.contactor.FirstOrDefault(v => v.idContactor == user.Contactor_idContactor);

				context.contactor.DeleteObject(contact);
				context.user.DeleteObject(user);

				context.SaveChanges();
			}

			UpdateUserSelectListBox();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cbGroup.SelectedItem as MyListBoxItem;
			if (mBoxItem == null)
			{
				return;
			}

			int id = int.Parse(mBoxItem.id);

			//delete group => user connection
			using (var context = new blackbirdEntities())
			{
				var group_users = context.group_user.Where(v => v.Group_idGroup == id);

				foreach (var gu in group_users)
				{
					context.group_user.DeleteObject(gu);
				}
				context.SaveChanges();

			}
			//delete group => action connection
			using (var context = new blackbirdEntities())
			{
				var group_users = context.group_action.Where(v => v.Group_idGroup == id);

				foreach (var gu in group_users)
				{
					context.group_action.DeleteObject(gu);
				}
				context.SaveChanges();

			}

			//delete group => process connection
			using (var context = new blackbirdEntities())
			{
				var group_users = context.process.Where(v => v.Group_idGroup == id);

				foreach (var gu in group_users)
				{
					context.process.DeleteObject(gu);
				}
				context.SaveChanges();
			}
	
			//delete group
			using (var context = new blackbirdEntities())
			{
				var gp = context.group.FirstOrDefault(v => v.idGroup == id);

				context.group.DeleteObject(gp);
				context.SaveChanges();
			}

			UpdateGroupSelectListBox();
		}

		private void FillOperationTypeCommonbox()
		{
			cmbSupportedtypelist.Items.Clear();
			using (var context = new blackbirdEntities())
			{
				var alltypess = context.processtype;
				foreach (var oneType in alltypess)
				{
					MyListBoxItem userItem = new MyListBoxItem();
					userItem.id = oneType.idProcessType.ToString();
					userItem.Context = oneType.Name;
					cmbSupportedtypelist.Items.Add(userItem);
				}
			}
		}

		private void cmbSupportedtypelist_SelectedIndexChanged(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cmbSupportedtypelist.SelectedItem as MyListBoxItem;
			if (mBoxItem == null)
			{
				return;
			}
			UpdateProcessStepBox(mBoxItem.Context);
		}


		private void btbijiadanprocessredesign_Click(object sender, EventArgs e)
		{
			MyListBoxItem mBoxItem = cmbSupportedtypelist.SelectedItem as MyListBoxItem;
			if (mBoxItem == null)
			{
				return;
			}

			ModifyProcessStep ms = new ModifyProcessStep(mBoxItem.Context);
			if (ms.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				UpdateProcessStepBox(mBoxItem.Context);
			}
		}

		private void UpdateProcessStepBox(string typename)
		{
			lbBijiadanprocessStep.Items.Clear();

			using (var context = new blackbirdEntities())
			{
				var processTypeId = context.processtype.SingleOrDefault(v => v.Name == typename);

				var processSteps = context.process.Where(v => v.ProcessType_idProcessType == processTypeId.idProcessType);

				var processLists = processSteps.ToList();

				processLists.Sort(ProcessComparision);

				for (int i = 0; i < processLists.Count; i++)
				{
					process ps = processLists[i];
					group gp = context.group.FirstOrDefault(v => v.idGroup == ps.Group_idGroup);

					MyListBoxItem gpItem = new MyListBoxItem();
					gpItem.id = gp.idGroup.ToString();
					gpItem.Context = gp.Name + " " + gp.Description;
					lbBijiadanprocessStep.Items.Add(gpItem);
				}

			}
		}

		private static int ProcessComparision(process x, process y)
		{
			if (x.StepNumber > y.StepNumber)
				return 1;
			if (x.StepNumber < y.StepNumber)
				return -1;

			return 0;
		}

		private void btAddActionToGroup_Click(object sender, EventArgs e)
		{
			MyListBoxItem mActionItem= cbActions.SelectedItem as MyListBoxItem;
			if (mActionItem == null)
				return;

			MyListBoxItem mgroupItem = cbGroup.SelectedItem as MyListBoxItem;
			if (mgroupItem == null)
				return;
			//Check if it alreay existed;
			foreach (var gaitem in lbGroupActions.Items)
			{
				MyListBoxItem gaListItem = gaitem as MyListBoxItem;
				if (gaListItem.id == mActionItem.id)
					return;
			}

			using (var context = new blackbirdEntities())
			{
				group_action ga = new group_action();

				ga.Action_idAction = int.Parse(mActionItem.id);
				ga.Group_idGroup = int.Parse(mgroupItem.id);

				context.group_action.AddObject(ga);

				context.SaveChanges();

				MyListBoxItem newItem = new MyListBoxItem();
				newItem.id = ga.id.ToString();
				newItem.Context = mActionItem.Context;
				lbGroupActions.Items.Add(newItem);
			}

		}

		private void btDeleteGroupAction_Click(object sender, EventArgs e)
		{
			MyListBoxItem selectedActionItem = lbGroupActions.SelectedItem as MyListBoxItem;
			if(selectedActionItem == null)
				return;

			int id = int.Parse(selectedActionItem.id);
			using (var context = new blackbirdEntities())
			{
				var gp_action = context.group_action.FirstOrDefault(v => v.id == id);

				context.group_action.DeleteObject(gp_action);

				context.SaveChanges();
				lbGroupActions.Items.Remove(selectedActionItem);
			}
		}



	}
}
